package com.junlaninfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by 辉 on 2020/7/29.
 */
public class TicketThread2 implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(TicketThread2.class);
    private ReentrantLock lock=new ReentrantLock();
    private  int count = 100;

    @Override
    public void run() {
        while (count > 0) {
            try {
                Thread.sleep(30);
            } catch (Exception e) {

            }

            ticket();
        }
    }


    private  void ticket() {
        if (count > 0) {
            lock.lock();  //加锁
            System.out.println(Thread.currentThread().getName() + ",正在开始出售:" + (100 - count + 1));
            count--;
            lock.unlock();//解锁
        }

    }

    public static void main(String[] args) {
        TicketThread2 ticketThread = new TicketThread2();
        Thread thread1 = new Thread(ticketThread, "窗口1");
        Thread thread2 = new Thread(ticketThread, "窗口2");
        thread1.start();
        thread2.start();
    }
}

